<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
         var list = [
            { id: 1, title: '小米', price: 2, num: 10 },
            { id: 2, title: '大米', price: 3, num: 5 },
            { id: 3, title: '黑米', price: 4, num: 10 },
            { id: 4, title: '糯米', price: 5, num: 10 }
        ]
    //     console.log(JSON.stringify(list))
    //     console.log(JSON.stringify(list))
    //     console.log(JSON.parse('[{"id":1,"title":"小米","price":2,"num":10},{"id":2,"title":"大米","price":3,"num":5},{"id":3,"title":"黑米","price":4,"num":10},{"id":4,"title":"糯米","price":5,"num":10}]'))
       
    //        var sum = list.reduce(function ( money, shop, index, array) {
    //            money += shop.price*shop.num
    //            return money
    //        },0)
    //    console.log(sum)

    console.log(JSON.stringify(list)) //将类数组对象转换成字符串
    console.log(JSON.parse('[{"id":1,"title":"小米","price":2,"num":10},{"id":2,"title":"大米","price":3,"num":5},{"id":3,"title":"黑米","price":4,"num":10},{"id":4,"title":"糯米","price":5,"num":10}]'))
    //将字符串重新转化为字符串  通常会在前后端交互的时候需要使用  在添加字符串是需要使用''
        var sum = list.reduce(function (money, shop) { //使用reduce计算价格 money和shop都是形参
           return money +=shop.price*shop.num   
        },0)
        console.log(sum)
    </script>
</body>
</html>